﻿/**
* @description  Holds an animation for a specified time before proceeding to the next tween.
*/
package com.foed.animation {

	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import com.foed.events.TweenEvent;

	public class AnimationHold extends Animator {
	
		/**
		* @description  Constructor.
		*
		* @param  pTime  The amount of time to hold, in milliseconds.
		*/
		public function AnimationHold(pTime:Number) {
			_timer = new Timer(pTime, 1);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onEndTween, false, 0, true);
		}
	
		/**
		* @description  Called once the hold timer completes.
		*
		* @param  pEvent  Event fired by Timer.
		*/
		private function onEndTween(pEvent:TimerEvent):void {
			halt();
			dispatchEvent(new TweenEvent(TweenEvent.END, this));
		}
	
		/**
		* @description  Stops the animation hold.
		*/
		override public function halt():void {
			if (_timer.running) _timer.stop();
		}
	
		/**
		* @description  Starts the animation hold.
		*/
		override public function start():void {
			dispatchEvent(new TweenEvent(TweenEvent.START, this));
			_timer.start();
		}
	
		/**
		* @description  Cleans up any timers that might be running.
		*/
		override public function die():void {
			_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onEndTween);
			super.die();
		}

	}
	
}